﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ETADTools.Lib
{
  public static class Formatter
  {
    /// <summary>
    /// Format Table Name
    /// </summary>
    /// <param name="tableName"></param>
    /// <returns></returns>
    public static string FormatTableName(string tableName)
    {
      if (tableName == string.Empty)
      {
        throw new ArgumentNullException("Table name");
      }

      // Remove first letter t in table name if existed
      if (tableName[0] == 't')
      {
        tableName = tableName.Substring(1, tableName.Length - 1);
      }

      return tableName;
    }

    /// <summary>
    /// Format PAGENAME
    /// </summary>
    /// <param name="tableName"></param>
    /// <returns></returns>
    public static string FormatPageName(string pageName)
    {
      if (pageName == string.Empty)
      {
        throw new ArgumentNullException("Page name");
      }

      // Remove extention asp in page name if existed
      string[] parts = pageName.Split(new char[]{'.'}, StringSplitOptions.RemoveEmptyEntries);
      if (parts.Length == 2)
      {
        if (parts[1] == "asp")
        {
          pageName = parts[0];
        }
      }

      return pageName;
    }

    /// <summary>
    /// Used for Export script builder
    /// </summary>
    /// <param name="fieldName"></param>
    /// <returns></returns>
    public static string FormatFieldName(string fieldName)
    {
      if(string.IsNullOrWhiteSpace(fieldName))
      {
        return string.Empty;
      }

      string result = string.Empty;
      
      // Remove escape characters
      fieldName = fieldName.Replace("-", "");
      fieldName = fieldName.Replace("/", "");
      fieldName = fieldName.Replace(".", "");
      fieldName = fieldName.Replace("(", "");
      fieldName = fieldName.Replace(")", "");
      fieldName = fieldName.Replace("%", "Percent");

      string[] parts = fieldName.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
      foreach (string part in parts)
      {
        result += char.ToUpper(part[0]) + part.Substring(1);
      }
      
      return result;
    }
  }
}
